package com.yzqp.format.weight;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yzqp.common.util.FastJsonUtil;
import com.yzqp.common.util.FileUtil;
import com.yzqp.format.BaseFormatUtil;

public class FormatWeightUtil extends BaseFormatUtil{

    public static String readUserPath = basePath + "weight_diary\\weight_diary_uniapp\\wx_json\\user.json";

    public static String writeUserPath = basePath + "weight_diary\\weight_diary_uniapp\\wx_json\\format_user.json";

    public static String readWeightPath = basePath + "weight_diary\\weight_diary_uniapp\\wx_json\\weight.json";

    public static String writeWeightPath = basePath + "weight_diary\\weight_diary_uniapp\\wx_json\\format_weight.json";

    public static void formatUser(){
        // 读取文件
        String readString = FileUtil.readFile(readUserPath);
        readString = wxJsonToCommonJson(readString);
        System.out.println(readString);
        JSONArray jsonArray = FastJsonUtil.stringToJSONArray(readString);

        // 写下文件
        for(int i = 0 ; i < jsonArray.size() ; i ++){
            /**

             _id  "629d6a6a5e068d00010b5106"
             env  "qq"
             openId  "52A005CCD7A8A485B2DE302CA7D5EEB1"
             avatarUrl  "https://thirdqq.qlogo.cn/qqapp/1112136237/52A005CCD7A8A485B2DE302CA7D5EEB1/100"
             nickName  "002"
             */
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            jsonObject.put("_id", jsonObject.getString("_id"));
            jsonObject.put("openId", jsonObject.get("_openid"));
            jsonObject.put("env", "weixin");
            jsonObject.put("nickName", jsonObject.get("nickName"));
            jsonObject.put("avatarUrl", jsonObject.get("avatarUrl"));
        }

        formatJson(writeUserPath, jsonArray);
    }



    public static void formatWeight(){
        // 读取文件
        String readString = FileUtil.readFile(readWeightPath);
        readString = wxJsonToCommonJson(readString);
        System.out.println(readString);
        JSONArray jsonArray = FastJsonUtil.stringToJSONArray(readString);

        // 写下文件
        for(int i = 0 ; i < jsonArray.size() ; i ++){


            /**
             _id  "6299c6f1b717e800012d418e"
             date  "2021-06-03"
             time  "16:31"
             dateTime  "2021-06-03T08:31:00.000Z"
             weight  77
             yearMonth  "2021-6"
             dateTimeStamp  1622709060000
             openId  "_0008JzJ1W4p7V8zwh3KUUv923YpVeYzQvXs"
             */
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            jsonObject.put("_id", jsonObject.getString("_id"));
            jsonObject.put("openId", jsonObject.get("_openid"));
            jsonObject.put("date", jsonObject.get("date"));
            jsonObject.put("time", jsonObject.get("time"));
            jsonObject.put("dateTime", jsonObject.get("dateTime"));
            jsonObject.put("weight", jsonObject.get("weight"));
            jsonObject.put("yearMonth", jsonObject.get("yearMonth"));
            jsonObject.put("dateTimeStamp", jsonObject.get("dateTimeStamp"));
        }

        formatJson(writeWeightPath, jsonArray);
    }

}
